Spring MVC এবং Spring Security একসাথে কাজ করার মাধ্যমে একটি ওয়েব অ্যাপ্লিকেশনকে সুরক্ষিত করা যায়। Spring Security একটি শক্তিশালী ফ্রেমওয়ার্ক যা অ্যাপ্লিকেশন নিরাপত্তা, যেমন Authentication, Authorization, CSRF protection, এবং Session management প্রদান করে। Spring MVC হল Spring এর ওয়েব ফ্রেমওয়ার্ক যা Model-View-Controller ডিজাইন প্যাটার্ন ব্যবহার করে।
Spring MVC তে Spring Security ইন্টিগ্রেট করার মাধ্যমে আপনি সহজেই ওয়েব অ্যাপ্লিকেশনের বিভিন্ন অংশে নিরাপত্তা প্রয়োগ করতে পারবেন।
Spring Security এবং Spring MVC সঠিকভাবে কাজ করার জন্য আপনাকে spring-boot-starter-security
ডিপেনডেন্সি যোগ করতে হবে।
Maven Dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
এই ডিপেনডেন্সিগুলি Spring Security এবং Spring MVC-এর কনফিগারেশন ও কার্যকারিতা সক্রিয় করবে।
Spring Security কনফিগারেশনে HTTP রিকোয়েস্ট নিরাপদ করতে HttpSecurity
কনফিগার করা হয় এবং AuthenticationManager
নির্ধারণ করা হয়।
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable() // CSRF protection বন্ধ করা (যদি প্রয়োজন হয়)
.authorizeRequests()
.antMatchers("/login", "/register").permitAll() // Login এবং Register পেজ সকলের জন্য খোলা
.anyRequest().authenticated() // অন্যান্য রিকোয়েস্টগুলো সুরক্ষিত
.and()
.formLogin()
.loginPage("/login") // Custom login page
.permitAll()
.and()
.logout()
.permitAll();
}
}
Explanation:
@EnableWebSecurity
: Spring Security সক্রিয় করে এবং সিস্টেমে নিরাপত্তা ব্যবস্থার কনফিগারেশন সম্পাদন করে।authorizeRequests()
: এই মেথডে আপনি যেই URL পাথগুলো সুরক্ষিত করতে চান এবং যেগুলো সকলের জন্য খোলা রাখতে চান তা কনফিগার করেন।formLogin()
: লগইন পেজ কাস্টমাইজ করার জন্য এটি ব্যবহৃত হয়।logout()
: লগআউট করার পদ্ধতি কনফিগার করা হয়।Spring MVC Controller তৈরি করা যাতে নিরাপদ রিকোয়েস্ট প্রক্রিয়া করা যায়। এখানে আমরা @Controller
অ্যানোটেশন ব্যবহার করব, যা Spring Security এর মাধ্যমে সুরক্ষিত।
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home() {
return "home"; // Home page (view name)
}
@GetMapping("/login")
public String login() {
return "login"; // Login page (view name)
}
}
Explanation:
@Controller
: এই ক্লাসটি Spring MVC Controller হিসেবে কাজ করবে এবং রিকোয়েস্টগুলিকে প্রক্রিয়া করবে।@GetMapping("/")
: "/"
পাথের জন্য GET রিকোয়েস্ট গ্রহণ করবে এবং ব্যবহারকারীকে home পেজ দেখাবে।Spring Security ডিফল্ট লগইন পেজ প্রদান করে, কিন্তু আপনি চাইলে একটি কাস্টম লগইন পেজ তৈরি করতে পারেন। নিচে একটি সিম্পল লগইন পেজের উদাহরণ দেওয়া হয়েছে:
login.jsp:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login Page</h2>
<form action="/login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<button type="submit">Login</button>
</form>
</body>
</html>
Explanation:
/login
URL-এ POST রিকোয়েস্ট পাঠাবে।Spring Security ডিফল্টভাবে In-Memory Authentication সরবরাহ করে। এটি একটি খুব সাধারণ পদ্ধতি যেখানে ইউজারনেম, পাসওয়ার্ড এবং রোল ইন-মেমোরি স্টোরেজে সংরক্ষিত থাকে।
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login", "/register").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().permitAll()
.and()
.logout().permitAll();
}
@Bean
@Override
public UserDetailsService userDetailsService() {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("admin").password("{noop}password").roles("ADMIN").build());
manager.createUser(User.withUsername("user").password("{noop}password").roles("USER").build());
return manager;
}
}
Explanation:
InMemoryUserDetailsManager
: এটি Spring Security-কে ইন-মেমরি ইউজার ডেটা ব্যবস্থাপনা করতে সাহায্য করে।User.withUsername
: ইউজারনেম, পাসওয়ার্ড এবং রোলস সহ ব্যবহারকারী তৈরি করা হয়।Spring Security তে রোল ভিত্তিক অনুমোদন কনফিগার করার জন্য hasRole
, hasAuthority
, বা permitAll()
ব্যবহার করা হয়।
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN") // Only ADMIN can access /admin
.antMatchers("/user/**").hasAnyRole("USER", "ADMIN") // Both USER and ADMIN can access /user
.anyRequest().authenticated() // All other requests need to be authenticated
.and()
.formLogin()
.permitAll()
.and()
.logout()
.permitAll();
}
}
Explanation:
hasRole("ADMIN")
: এই URL পাথ (যেমন /admin/**
) শুধুমাত্র ADMIN
রোলের ব্যবহারকারীরা অ্যাক্সেস করতে পারবে।hasAnyRole("USER", "ADMIN")
: এই URL পাথ /user/**
উভয় USER
এবং ADMIN
রোলের ব্যবহারকারীদের জন্য খোলা থাকবে।Spring Security Integration with Spring MVC দ্বারা আপনার ওয়েব অ্যাপ্লিকেশন নিরাপত্তা প্রদান করা সম্ভব হয়। Spring Security ব্যবহার করে আপনি Authentication এবং Authorization কনফিগার করতে পারেন এবং কাস্টম লগইন পেজ, রোল ভিত্তিক এক্সেস কন্ট্রোল, এবং নিরাপত্তার অন্যান্য বৈশিষ্ট্য যোগ করতে পারেন। Spring MVC এবং Spring Security একত্রে একটি শক্তিশালী এবং নিরাপদ ওয়েব অ্যাপ্লিকেশন তৈরি করার জন্য প্রযোজ্য।
Spring Security এবং Spring MVC একসাথে ব্যবহার করে একটি সুরক্ষিত এবং স্কেলেবল ওয়েব অ্যাপ্লিকেশন তৈরি করা যায়। Spring Security হল একটি শক্তিশালী ফ্রেমওয়ার্ক যা ওয়েব অ্যাপ্লিকেশনগুলিতে Authentication, Authorization, এবং Security নিয়ন্ত্রণ করে, আর Spring MVC হল একটি ফ্রেমওয়ার্ক যা অ্যাপ্লিকেশনটির আর্কিটেকচার এবং UI রেন্ডারিংয়ের জন্য ব্যবহৃত হয়।
Spring Security এর সাথে Spring MVC ইন্টিগ্রেট করার মাধ্যমে, আপনি নিরাপত্তা বৈশিষ্ট্য যেমন লগইন, লগআউট, রোল ভিত্তিক অ্যাক্সেস কন্ট্রোল এবং অনেক কিছু সহজেই কনফিগার করতে পারবেন।
Spring MVC অ্যাপ্লিকেশনগুলিতে Spring Security কনফিগার করার জন্য, আমরা Java Configuration এবং XML Configuration উভয়ই ব্যবহার করতে পারি। তবে বর্তমানে Java Configuration অধিক ব্যবহৃত হয়।
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// Custom UserDetailsService for Authentication
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home", "/login").permitAll() // Public pages
.antMatchers("/admin/**").hasRole("ADMIN") // Only ADMIN can access /admin/**
.anyRequest().authenticated() // Other requests need authentication
.and()
.formLogin()
.loginPage("/login") // Custom login page
.permitAll()
.and()
.logout()
.permitAll()
.and()
.csrf().disable(); // Disable CSRF for simplicity (this is optional)
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService); // Set custom userDetailsService for authentication
}
}
ব্যাখ্যা:
Spring MVC এর কন্ট্রোলার তৈরি করা যাক যেখানে Spring Security এর সাথে ইন্টিগ্রেশন করা হবে।
@Controller
public class HomeController {
@GetMapping("/home")
public String home(Model model) {
model.addAttribute("message", "Welcome to the Home page!");
return "home"; // home.jsp
}
@GetMapping("/admin")
public String admin(Model model) {
model.addAttribute("message", "Welcome to the Admin page!");
return "admin"; // admin.jsp
}
@GetMapping("/login")
public String login() {
return "login"; // login.jsp
}
}
login.jsp (লগইন পেজ):
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Please login to access the application</h2>
<form action="<c:url value='/login'/>" method="post">
<label for="username">Username: </label>
<input type="text" id="username" name="username" required /><br>
<label for="password">Password: </label>
<input type="password" id="password" name="password" required /><br>
<button type="submit">Login</button>
</form>
</body>
</html>
Spring Security লগআউট ফিচারটিও সরবরাহ করে, যা কন্ট্রোলার এবং ভিউ-এর মাধ্যমে ব্যবহারকারীর সেশন শেষ করতে সহায়ক।
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login", "/home").permitAll() // Public pages
.anyRequest().authenticated() // Other requests need authentication
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/home")
.permitAll();
}
Spring MVC এবং Spring Security ইন্টিগ্রেশন একটি সুরক্ষিত এবং স্কেলেবল ওয়েব অ্যাপ্লিকেশন তৈরির জন্য খুবই উপকারী। Spring Security Spring MVC এর সাথে ব্যবহার করে আপনি অ্যাপ্লিকেশনকে শক্তিশালী নিরাপত্তা ব্যবস্থা প্রদান করতে পারেন যেমন লগইন, লগআউট, রোল ভিত্তিক অ্যাক্সেস কন্ট্রোল এবং আরও অনেক কিছু। Spring MVC এর সহজ কনফিগারেশন এবং Spring Security এর শক্তিশালী নিরাপত্তা ফিচার একত্রিত করে একটি নিরাপদ এবং রক্ষণাবেক্ষণযোগ্য অ্যাপ্লিকেশন তৈরি করা যায়।
Spring Security তে access control করতে আমরা @PreAuthorize এবং @Secured অ্যানোটেশন ব্যবহার করতে পারি। এই অ্যানোটেশনগুলি মেথড লেভেলে ব্যবহার করা হয়, যার মাধ্যমে নির্দিষ্ট রোল বা অনুমতির ভিত্তিতে রিকোয়েস্টের অ্যাক্সেস কন্ট্রোল করা যায়।
@Secured অ্যানোটেশনটি Spring Security-তে ব্যবহারকারীর রোল (roles) অনুযায়ী মেথড অ্যাক্সেস কন্ট্রোল করার জন্য ব্যবহৃত হয়। এই অ্যানোটেশনটি মেথডের আগে ব্যবহার করা হয়, যা একটি নির্দিষ্ট রোলের অধিকারী ব্যবহারকারী ছাড়া অন্যদের অ্যাক্সেস বন্ধ করে।
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Secured("ROLE_ADMIN")
public void deleteUser() {
System.out.println("Admin deleted the user.");
}
@Secured({"ROLE_USER", "ROLE_ADMIN"})
public void viewUser() {
System.out.println("User details viewed.");
}
}
Spring Security তে @Secured অ্যানোটেশন ব্যবহার করার জন্য @EnableGlobalMethodSecurity কনফিগারেশন প্রয়োজন।
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true) // Enable @Secured
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// Security configuration
}
@PreAuthorize অ্যানোটেশনটি একটি শক্তিশালী এবং নমনীয় পদ্ধতি, যা Spring Expression Language (SpEL) ব্যবহার করে কাস্টম শর্তাবলী নির্ধারণ করতে সহায়ক। এটি মেথডের আগে এক্সপ্রেশন চেক করে অ্যাক্সেস নিয়ন্ত্রণ করে।
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
@Service
public class ProductService {
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void createProduct() {
System.out.println("Product created by Admin.");
}
@PreAuthorize("hasPermission(#product, 'read')")
public void viewProduct(Product product) {
System.out.println("Product details viewed by the user.");
}
@PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
public void updateProduct() {
System.out.println("Product updated by User or Admin.");
}
}
read
পারমিশন চেক করবে এবং কেবলমাত্র সেই ব্যবহারকারী যার কাছে এই পারমিশন রয়েছে, সে viewProduct() মেথডটি অ্যাক্সেস করতে পারবে।Spring Security তে @PreAuthorize ব্যবহার করতে @EnableGlobalMethodSecurity(prePostEnabled = true) কনফিগারেশন করতে হয়।
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // Enable @PreAuthorize
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// Security configuration
}
Feature | @Secured | @PreAuthorize |
---|---|---|
Basic Purpose | রোল ভিত্তিক অ্যাক্সেস কন্ট্রোল | SpEL এর মাধ্যমে কাস্টম শর্তাবলী দিয়ে অ্যাক্সেস কন্ট্রোল |
Expression Support | No | Yes (SpEL expressions support) |
Use Case | সহজ রোল ভিত্তিক নিরাপত্তা নিয়ন্ত্রণ | কাস্টম শর্তাবলী, পারমিশন চেক, এবং আরও উন্নত কন্ট্রোল |
Example | @Secured("ROLE_ADMIN") | @PreAuthorize("hasRole('ROLE_ADMIN')") |
@Secured("ROLE_ADMIN")
public void deleteProduct() {
// Only Admin can delete product
}
@PreAuthorize("hasRole('ROLE_ADMIN') and #product.price > 1000")
public void discountProduct(Product product) {
// Admin can discount products with price greater than 1000
}
@PreAuthorize("hasRole('ROLE_ADMIN') or (hasRole('ROLE_USER') and #product.owner == authentication.name)")
public void updateProductDetails(Product product) {
// Admin can update, or User can update their own product
}
Spring Security তে @Secured এবং @PreAuthorize অ্যানোটেশন দুটি powerful tool যা মেথড লেভেলে access control পরিচালনা করতে সহায়ক। @Secured সোজা রোল ভিত্তিক কন্ট্রোল প্রদান করে, যেখানে @PreAuthorize অধিক নমনীয় এবং Spring Expression Language (SpEL) এর মাধ্যমে কাস্টম access control তৈরির সুযোগ দেয়।
@PreAuthorize এবং @Secured এর মাধ্যমে আপনি Spring MVC অ্যাপ্লিকেশনে শক্তিশালী এবং নমনীয় নিরাপত্তা ব্যবস্থা তৈরি করতে পারেন।
Spring Security এবং Spring MVC একত্রে ব্যবহৃত হলে, এটি ওয়েব অ্যাপ্লিকেশনের নিরাপত্তা প্রদান করে এবং একই সময়ে ডেটা বা রিসোর্সের অ্যাক্সেস নিয়ন্ত্রণ করে। Spring MVC ওয়েব রিকোয়েস্ট পরিচালনার জন্য এবং Spring Security ব্যবহারকারীর প্রমাণীকরণ (authentication) ও অনুমোদন (authorization) নিয়ন্ত্রণ করার জন্য ব্যবহৃত হয়।
এখানে আমরা একটি সাধারন উদাহরণ দেখাবো যেখানে Spring Security এবং Spring MVC একত্রে ব্যবহৃত হয়েছে:
প্রথমে, Spring Boot অ্যাপ্লিকেশনের জন্য প্রয়োজনীয় ডিপেনডেন্সি যোগ করতে হবে:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
spring-boot-starter-web
: Spring MVC এবং RESTful অ্যাপ্লিকেশন তৈরি করতে ব্যবহার হয়।spring-boot-starter-security
: Spring Security ব্যবহারের জন্য।spring-boot-starter-thymeleaf
: UI টেমপ্লেট ইঞ্জিন হিসেবে Thymeleaf ব্যবহারের জন্য।Spring Security কনফিগারেশন তৈরির জন্য একটি কনফিগারেশন ক্লাস তৈরি করতে হবে যেখানে আপনি authentication এবং authorization কনফিগার করবেন।
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.memory.InMemoryUserDetailsManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public UserDetailsService userDetailsService() {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
// Define in-memory users with roles
manager.createUser(User.withUsername("admin").password(passwordEncoder().encode("admin123")).roles("ADMIN").build());
manager.createUser(User.withUsername("user").password(passwordEncoder().encode("user123")).roles("USER").build());
return manager;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(); // Use BCryptPasswordEncoder for password encoding
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN") // Only accessible by ADMIN role
.antMatchers("/user/**").hasAnyRole("USER", "ADMIN") // Accessible by USER and ADMIN roles
.anyRequest().authenticated() // All other requests need authentication
.and()
.formLogin()
.loginPage("/login") // Custom login page
.permitAll()
.and()
.logout()
.permitAll();
return http.build();
}
}
InMemoryUserDetailsManager
: এটি ব্যবহারকারী তথ্য (যেমন username, password, roles) ইন-মেমরি (RAM) সংরক্ষণ করে।BCryptPasswordEncoder
: এটি নিরাপদ পাসওয়ার্ড এনকোডিং এর জন্য ব্যবহৃত হয়।HttpSecurity
: এটি নিরাপত্তা কনফিগারেশন করতে ব্যবহৃত হয়, যেমন formLogin()
দিয়ে কাস্টম লগিন পেজ তৈরি করা এবং authorizeRequests()
দিয়ে URL পাথের জন্য রোল ভিত্তিক অনুমতি নির্ধারণ করা।এখন Spring MVC কন্ট্রোলার তৈরি করি যেখানে আপনি ভিউ এবং রিকোয়েস্ট হ্যান্ডলিং করবেন।
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/home")
public String home() {
return "home"; // Returns 'home.html' or 'home.jsp'
}
@GetMapping("/user/dashboard")
public String userDashboard() {
return "user/dashboard"; // Returns 'user/dashboard.html'
}
@GetMapping("/admin/dashboard")
public String adminDashboard() {
return "admin/dashboard"; // Returns 'admin/dashboard.html'
}
@GetMapping("/login")
public String login() {
return "login"; // Returns 'login.html'
}
}
@RequestMapping("/home")
: সাধারণ ব্যবহারকারীর জন্য হোম পেজ রেন্ডার করার জন্য।@GetMapping("/user/dashboard")
: USER রোলের জন্য ড্যাশবোর্ড পেজ।@GetMapping("/admin/dashboard")
: ADMIN রোলের জন্য ড্যাশবোর্ড পেজ।Thymeleaf ব্যবহার করে HTML টেমপ্লেট তৈরি করা হবে, যাতে ইউজার ইন্টারফেস তৈরি করা যাবে।
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home Page</title>
</head>
<body>
<h1>Welcome to the Home Page!</h1>
<p><a href="/user/dashboard">Go to User Dashboard</a></p>
<p><a href="/admin/dashboard">Go to Admin Dashboard</a></p>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Login Page</title>
</head>
<body>
<h1>Please Log In</h1>
<form action="/login" method="post">
<label>Username:</label><input type="text" name="username" /><br />
<label>Password:</label><input type="password" name="password" /><br />
<button type="submit">Log In</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>User Dashboard</title>
</head>
<body>
<h1>Welcome, User!</h1>
<p>You have access to the User Dashboard.</p>
<a href="/home">Back to Home</a>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Admin Dashboard</title>
</head>
<body>
<h1>Welcome, Admin!</h1>
<p>You have access to the Admin Dashboard.</p>
<a href="/home">Back to Home</a>
</body>
</html>
Spring Boot অ্যাপ্লিকেশন রান করতে MyApplication.java
ক্লাস তৈরি করুন:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@SpringBootApplication
: এটি Spring Boot অ্যাপ্লিকেশন কনফিগারেশনের মূল অ্যানোটেশন যা সমস্ত প্রয়োজনীয় কনফিগারেশন সঠিকভাবে লোড করবে।এই উদাহরণটি দেখায় কীভাবে Spring Security এবং Spring MVC একত্রে ব্যবহৃত হয়:
এই উদাহরণের মাধ্যমে আপনি Spring Security এবং Spring MVC এর মধ্যে ইন্টিগ্রেশন করতে পারবেন, যা আপনাকে নিরাপদ এবং শক্তিশালী ওয়েব অ্যাপ্লিকেশন তৈরি করতে সহায়তা করবে।
Read more